﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Forms;
using Marmat.Forms.Skin;
using Marmat.Foundation;

namespace Marmat.Template.WinForm
{
    /// <summary>
    /// 紫色登录窗体
    /// </summary>
    public partial class PurpleLoginForm : NoneBorderForm, ILoginForm
    {

        /// <summary>
        /// 构建登录窗体的实例对象。
        /// </summary>
        protected PurpleLoginForm() : this(null)
        {
        }

        /// <summary>
        /// 构建登录窗体的实例对象。
        /// </summary>
        /// <param name="login"></param>
        public PurpleLoginForm(ILogin login)
        {
            InitializeComponent();
            this.ProductName = Application.ProductName;
            this.Login = login;
            this.Icon = FileIconAPI.GetIcon(Application.ExecutablePath);
            this.DragParentForm();
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        protected ILogin Login { get; }

        /// <summary>
        /// 获取返回的用户ID。
        /// </summary>
        protected string UserID { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public new virtual string ProductName
        {
            get => this._productName;
            set => this._productName = value;
        }
        private string _productName = "您的公司名称";

        /// <summary>
        /// 标题图标-建议大小：50 X 27 像素
        /// </summary>
        public Image TitleIcon
        {
            get => this._titleIcon;
            set => this._titleIcon = value;
        }
        private Image _titleIcon = Properties.Resources.im大;

        /// <summary>
        /// 显示登录窗体
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public bool ShowDialog(out string user_id)
        {
            user_id = default;
            var dialog = this.ShowDialog();
            bool res = dialog == DialogResult.OK;
            if (res)
                user_id = this.UserID;

            if (this.checkBox1.Checked && res)
            {
                Properties.Settings.Default.PassWord = this.PassWord.Text.Trim();
                Properties.Settings.Default.UserName = this.UserName.Text.Trim();
            }
            Properties.Settings.Default.RememberPassWord = this.checkBox1.Checked;
            Properties.Settings.Default.Save();
            return res;
        }



        #region 绘制代码

        /// <summary>
        /// 鼠标进入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginBtn_MouseEnter(object sender, EventArgs e)
        {
            this.LoginBtn.BackColor = Color.FromArgb(163, 117, 216);
        }

        /// <summary>
        /// 鼠标离开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginBtn_MouseLeave(object sender, EventArgs e)
        {
            //123, 77, 176
            this.LoginBtn.BackColor = Color.FromArgb(143, 97, 176);
        }

        /// <summary>
        /// 按下鼠标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginBtn_MouseDown(object sender, MouseEventArgs e)
        {
            this.LoginBtn.BackColor = Color.FromArgb(113, 67, 166);
        }

        /// <summary>
        /// 松开鼠标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginBtn_MouseUp(object sender, MouseEventArgs e)
        {
            this.LoginBtn.BackColor = Color.FromArgb(143, 97, 176);
        }

        /// <summary>
        /// 窗体绘制事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PurpleLoginForm_Paint(object sender, PaintEventArgs e)
        {
            var rec = new Rectangle(609, 80, 395, 48);
            var font = new Font("微软雅黑", 24F, FontStyle.Bold);
            TextRenderer.DrawText(e.Graphics, this.ProductName, font, rec, Color.FromArgb(123, 77, 176), TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
        }
        #endregion

        private async void LoginBtn_Click(object sender, EventArgs e)
        {
            await this.LoginSystemAsync();
        }

        /// <summary>
        /// 登录系统
        /// </summary>
        private async Task LoginSystemAsync()
        {
            this.TrompTitle.Text = null;
            string username = this.UserName.Text.Trim();
            string password = this.PassWord.Text.Trim();
            if (string.IsNullOrEmpty(username))
            {
                this.TrompTitle.Text = "请输入用户名";
                this.UserName.Focus();
            }

            else if (string.IsNullOrEmpty(password))
            {
                this.TrompTitle.Text = "请输入密码";
                this.PassWord.Focus();
            }
            else
            {
                if (this.Login != null)
                {
                    try
                    {
                        this.LoginBtn.Enabled = false;
                        this.UserID = await this.Login.Login(username, password);
                        this.DialogResult = DialogResult.OK;
                    }
                    catch (Exception ex)
                    {
                        this.TrompTitle.Text = ex.GetBaseException().Message;
                    }
                    finally
                    {
                        this.LoginBtn.Enabled = true;
                    }
                }
            }
        }


        private void PurpleLoginForm_Load(object sender, EventArgs e)
        {
            this.TitleIconLab.Image = this.TitleIcon;
            var result = Properties.Settings.Default.RememberPassWord;
            if (result)
            {
                this.UserName.Text = Properties.Settings.Default.UserName;
                this.PassWord.Text = Properties.Settings.Default.PassWord;
                this.checkBox1.Checked = result;
            }
        }

        private async void PurpleLoginForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) await this.LoginSystemAsync();
        }
    }
}
